//
//  NetRequest.m
//  SYproject1
//
//  Created by mac_chen on 15/12/9.
//  Copyright © 2015年 mac_chen. All rights reserved.
//

#import "NetRequest.h"
#import "MBProgressHUD.h"
@implementation NetRequest

static NetRequest *_instance;

+ (NetRequest *)shareHandle
{
    @synchronized(self) {
        if (_instance == nil) {
            _instance = [[super allocWithZone:NULL] init];
        }
    }
    return _instance;
}

- (id)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)GET:(NSString *)api completion:(void (^)(id, id, NSError *))completion
{
    if (api && [api length] > 0) {
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/json", @"application/json", @"text/javascript", @"text/html", nil];
        
        [manager GET:api parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [netWorkManager stopMonitoring];
            completion(operation, responseObject, nil);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            [alert show];
            completion(operation, nil, error);
            
        }];
//        [manager POST:api parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//            
//            [netWorkManager stopMonitoring];
//            completion(operation, responseObject, nil);
//            
//        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//            
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无网络访问" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
//                [alert show];
//                completion(operation, nil, error);
//            
//        }];
    }
}

@end
